﻿/* Write a method that checks if the element at given position in given array of integers is bigger than its two neighbors 
 * (when such exist).
 */




using System;

class CheckTheNeighbours
{
    static void Main()
    {
        int N;
        uint array_size, checkPos;

        bool iProvided = false;
        do
        {
            if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter a POSITIVE integer. \r\nValid range: {0} - {1}", 1, UInt32.MaxValue);
            else iProvided = true;
            Console.Write("Please enter how many members you want to add: ");
        } while (UInt32.TryParse(Console.ReadLine(), out array_size) == false || array_size < 1);

        int[] nums = new int[array_size];
        for (int i = 0; i < array_size; i++)
        {
            iProvided = false;         //reset iProvided to avoid getting again the error message from a previous error check
            do
            {
                if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter an integer. \r\n\tValid range: {0} - {1}", Int32.MinValue, Int32.MaxValue);
                else iProvided = true;
                Console.Write("Please enter number #{0}: ", i + 1);
            } while (Int32.TryParse(Console.ReadLine(), out nums[i]) == false);
            //Console.WriteLine(nums[i]);
        }

        iProvided = false;         //reset iProvided to avoid getting again the error message from a previous error check
        do
        {
            if (iProvided == true) Console.WriteLine("Error!: Invalid input. Please enter an integer in the range of array. \r\nValid range: 0 - {0}", nums.Length-1);
            else iProvided = true;
            Console.Write("Check element at position: ");
        } while (UInt32.TryParse(Console.ReadLine(), out checkPos) == false || checkPos < 0 || checkPos > nums.Length-1);

        int bigger_than_its_neighbours = CheckIfBigger(checkPos, nums);
        if (bigger_than_its_neighbours == 0) Console.WriteLine("None is bigger");
        if (bigger_than_its_neighbours == -1) Console.WriteLine("Only left one is bigger!");
        if (bigger_than_its_neighbours == 1) Console.WriteLine("Only right one is bigger!");
        if (bigger_than_its_neighbours == 2) Console.WriteLine("Both are bigger!");


    }

    /* method to check if the element at checkPos is bigger than elements checkPos-1 and checkPos+1 (its neighbours)
     * return values: 0 if no one is bigger or neighbours do not exist, 2 if both are bigger, -1 if just left is bigger, 1 if right is bigger
     */
    /* note: malko sym se poobyrkal ot uslovieto na zadachata i veroqtno shteshe da e po-pravilno da se pravqt protivopolojnite proverki, razbral sym che trqbva da se proverqva dali sysednite elementi
     * sa po-malki, no taka ili inache kakto sym napisal metoda funkcionalnostta vaji (kakto se vijda v sledvashtata zadacha) */
    private static int CheckIfBigger(uint checkPos, int[] nums)
    {
        int? returnCode = null;

        if (checkPos < 0 || checkPos > nums.Length - 1) return 0; //there are no such elements, do not go on

        int left_neighbour, right_neighbour;

        //case if there is no right neighbour
        if (checkPos == nums.Length - 1)
        {
            left_neighbour = nums[checkPos - 2];
            if (left_neighbour > nums[checkPos]) return 1;
            else return 0;
        }

        //case if there is no left neighbour
        if (checkPos == 0) 
        {
            right_neighbour = nums[checkPos + 1];
            if (right_neighbour > nums[checkPos]) return 1;
            else return 0;
        }

        left_neighbour = nums[checkPos - 1];
        right_neighbour = nums[checkPos + 1];

        //check cases
        returnCode = 0; //by default none is bigger
        
        //check other cases
        if (left_neighbour > nums[checkPos] && right_neighbour <= nums[checkPos]) returnCode = -1;
        else if (left_neighbour <= nums[checkPos] && right_neighbour > nums[checkPos]) returnCode = 1;
        else if (left_neighbour < nums[checkPos] && right_neighbour <= nums[checkPos]) returnCode = 0;
        else if (left_neighbour <= nums[checkPos] && right_neighbour < nums[checkPos]) returnCode = 0;
        else if (left_neighbour < nums[checkPos] && right_neighbour < nums[checkPos]) returnCode = 0;
        else if (left_neighbour > nums[checkPos] && right_neighbour > nums[checkPos]) returnCode = 2;
        else returnCode = 0;

        return (int)returnCode;
    }
}
